<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="报工号" prop="reportWorkNo">
        <el-input v-model="queryParams.reportWorkNo" placeholder="请输入报工号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工单号" prop="workOrderNo">
        <el-input v-model="queryParams.workOrderNo" placeholder="请输入工单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="计划编号" prop="planNo">
        <el-input v-model="queryParams.planNo" placeholder="请输入计划编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="车间" prop="deptId">
        <treeselect style="width: 200px" v-model="queryParams.deptId" :options="deptList" :normalizer="normalizer"
                    placeholder="请选择车间" clearable/>
      </el-form-item>
      <el-form-item label="物料编码" prop="materialNo">
        <el-input v-model="queryParams.materialNo" placeholder="请输入物料编码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工序编号" prop="procedureCode">
        <el-input v-model="queryParams.procedureCode" placeholder="请输入工序编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工序名称" prop="procedureName">
        <el-input v-model="queryParams.procedureName" placeholder="请输入工序名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="报工产品状态" prop="reportStatus">
        <el-select v-model="queryParams.reportStatus" placeholder="请选择报工产品状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.REPORT_STATUS)" :key="dict.value" :label="dict.label"
                     :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :label="dict.label"
                     :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                   v-hasPermi="['production:report-work:create']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['production:report-work:export']">导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-document"
          size="mini"
          @click="handlePrint"
          :disabled="checkedIds.length !== 1"
          v-hasPermi="['production:report-work:export']">打印
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['production:report-work:delete']"
        >
          批量删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55"/>
      <el-table-column label="报工号" align="center" prop="reportWorkNo" width="250"/>
      <el-table-column label="工单号" align="center" prop="workOrderNo" width="200"/>
      <el-table-column label="批次号" align="center" prop="batchNo" width="200"/>
      <el-table-column label="计划编号" align="center" prop="planNo" width="200"/>
      <el-table-column label="车间名称" align="center" prop="deptName" width="200"/>
      <el-table-column label="物料编码" align="center" prop="materialNo" width="200"/>
      <el-table-column label="物料名称" align="center" prop="materialName" width="200"/>
      <el-table-column label="工序编号" align="center" prop="procedureCode" width="200"/>
      <el-table-column label="工序名称" align="center" prop="procedureName" width="200"/>
      <el-table-column label="设备编号" align="center" prop="deviceCode" width="200"/>
      <el-table-column label="设备名称" align="center" prop="deviceName" width="200"/>
      <el-table-column label="基本单位" align="center" prop="basicUnit" width="200"/>
      <el-table-column label="报工数量" align="center" prop="reportQuantity" width="200"/>
      <el-table-column label="报工产品状态" align="center" prop="reportStatus" width="200">
        <template v-slot="scope">
          <span>{{ getDictDataLabel(DICT_TYPE.REPORT_STATUS, scope.row.reportStatus) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班次名称" align="center" prop="shiftName" width="200"/>
      <el-table-column label="班组名称" align="center" prop="teamName" width="200"/>
      <el-table-column label="报工人名称" align="center" prop="reportUserName" width="200"/>
      <el-table-column label="报工时间" align="center" prop="reportTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.reportTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属日期" align="center" prop="scheduleDate" width="200"/>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="creator" width="120"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template v-slot="scope">-->
      <!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"-->
      <!--                     v-hasPermi="['production:report-work:update']">修改</el-button>-->
      <!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
      <!--                     v-hasPermi="['production:report-work:delete']">删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <ReportWorkForm ref="formRef" @success="getList"/>
  </div>
</template>

<script>
import * as ReportWorkApi from '@/api/production/reportwork';
import ReportWorkForm from './ReportWorkForm.vue';
import {printReportWork} from './print.js';
import {listDept} from "@/api/system/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "ReportWork",
  components: {
    ReportWorkForm,
    Treeselect

  },
  data() {
    return {
      deptList: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报工列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      checkedIds: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        reportWorkNo: null,
        workOrderNo: null,
        workOrderId: null,
        procedureWorkOrderId: null,
        planId: null,
        planNo: null,
        deptId: null,
        deptName: null,
        materialId: null,
        materialNo: null,
        materialName: null,
        procedureId: null,
        procedureCode: null,
        procedureName: null,
        deviceId: null,
        deviceCode: null,
        deviceName: null,
        reportStatus: null,
        batchNo: null,
        shiftId: null,
        shiftName: null,
        teamId: null,
        teamName: null,
        reportUserId: null,
        reportUserName: null,
        reportTime: [],
        scheduleDate: [],
        status: null,
        createTime: [],
      },
    };
  },
  created() {
    this.getList();
    this.getDeptList();

  },
  methods: {
    /** 获取部门列表 */
    getDeptList() {
      listDept().then(response => {
        this.deptList = this.handleTree(response.data, "id")[0]?.children || [];
      });
    },

    /** 部门节点格式化 */
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await ReportWorkApi.getReportWorkPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除报工编号为"' + id + '"的数据项?')
      try {
        await ReportWorkApi.deleteReportWork(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 批量删除报工 */
    async handleDeleteBatch() {
      await this.$modal.confirm('是否确认删除?')
      try {
        await ReportWorkApi.deleteReportWorkList(this.checkedIds);
        this.checkedIds = [];
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    handleRowCheckboxChange(records) {
      this.checkedIds = records.map((item) => item.id);
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有报工数据项?');
      try {
        this.exportLoading = true;
        const data = await ReportWorkApi.exportReportWorkExcel(this.queryParams);
        this.$download.excel(data, '报工.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },

    /** 打印按钮操作 */
    async handlePrint() {
      if (this.checkedIds.length !== 1) {
        this.$modal.msgWarning('请选择一行数据进行打印');
        return;
      }

      try {
        // 获取选中行的数据
        const selectedRow = this.list.find(item => item.id === this.checkedIds[0]);
        if (!selectedRow) {
          this.$modal.msgError('未找到选中的数据');
          return;
        }

        // 调用打印函数
        await printReportWork(selectedRow);
      } catch (error) {
        console.error('打印失败:', error);
        this.$modal.msgError('打印失败，请重试');
      }
    },
  }
};
</script>
